package com.bl.weilan.service.biz.companyopponent.impl;

import com.bl.weilan.common.ApiResponseCode;
import com.bl.weilan.common.CommonServiceException;
import com.bl.weilan.common.utils.ListUtils;
import com.bl.weilan.common.utils.UUIDUtils;
import com.bl.weilan.dao.model.CompanyEntity;
import com.bl.weilan.dao.model.CompanyFinancingEntity;
import com.bl.weilan.dao.model.CompanyOpponentEntity;
import com.bl.weilan.dao.model.TradeDEntity;
import com.bl.weilan.dao.repository.CompanyFinancingRepository;
import com.bl.weilan.dao.repository.CompanyOpponentRepository;
import com.bl.weilan.dao.repository.CompanyRepository;
import com.bl.weilan.dao.repository.TradeDRepository;
import com.bl.weilan.service.biz.companyopponent.CompanyOpponentService;
import com.bl.weilan.service.vo.DelBody;
import com.bl.weilan.service.vo.DelListBody;
import com.bl.weilan.service.vo.companyopponent.CompanyOpponentAddVo;
import com.bl.weilan.service.vo.companyopponent.CompanyOpponentListVo;
import com.bl.weilan.service.vo.companyopponent.CompanyOpponentModVo;
import com.bl.weilan.service.vo.companyopponent.CompanyOpponentVo;
import com.bl.weilan.service.vo.traded.TradeDDelBody;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Log4j2
public class CompanyOpponentServiceImpl implements CompanyOpponentService {
    @Autowired
    CompanyOpponentRepository companyOpponentRepository;
    @Autowired
    CompanyRepository companyRepository;
    @Autowired
    CompanyFinancingRepository companyFinancingRepository;

    @Autowired
    TradeDRepository tradeDRepository;

    @Override
    public CompanyOpponentEntity getCompanyOpponentById(String companyopponentId) {
        CompanyOpponentEntity companyopponentEntity = companyOpponentRepository.findOne(companyopponentId);
        return companyopponentEntity;
    }

    @Override
    public CompanyOpponentListVo getCompanyOpponentLists(String companyId, String tradeDId,Pageable pageable ){
        // 竞争对手
        CompanyOpponentListVo companyOpponentListVo = new CompanyOpponentListVo();
        List<CompanyOpponentVo> companyOpponentVos = new ArrayList<>();

        Page<CompanyOpponentEntity> companyOpponentEntities;

        companyOpponentEntities = companyOpponentRepository.findByTradeDId(tradeDId, pageable);
        if (null !=companyOpponentEntities){
            for (CompanyOpponentEntity opEntity : companyOpponentEntities) {
                String compid = opEntity.getCompanyId();
                if(compid.equals(companyId)) {
                    continue;
                }else {
                    CompanyEntity companyEntity1 = companyRepository.findOne(opEntity.getCompanyId());
                    CompanyOpponentVo companyOpponentVo = new CompanyOpponentVo();
                    BeanUtils.copyProperties(companyEntity1, companyOpponentVo);
                    List<CompanyFinancingEntity> companyFinancingEntities;
                    companyFinancingEntities = companyFinancingRepository.findByCompanyIdOrderByTimeDesc(opEntity.getCompanyId());
//                    if (companyFinancingEntities != null){
//                        companyOpponentVo.setFinancList(ListUtils.getFirst(companyFinancingEntities));
//                    }
                    CompanyFinancingEntity companyFinancingEntity = new CompanyFinancingEntity();
                    if(companyFinancingEntities.size() > 0) {
                        companyFinancingEntity = ListUtils.getFirst(companyFinancingEntities);
                        companyOpponentVo.setRecentFinancing(companyFinancingEntity.getTime());
                        companyOpponentVo.setRecentRound(companyFinancingEntity.getRounds());
                        companyOpponentVo.setRecentInvestor(companyFinancingEntity.getInvestor());
                        companyOpponentVo.setRecentMoney(companyFinancingEntity.getMoney());
                    }

                    companyOpponentVos.add(companyOpponentVo);
                }
            }
            companyOpponentListVo.setList(companyOpponentVos);
            companyOpponentListVo.setTotalCount(companyOpponentEntities.getTotalElements());
        }


        return companyOpponentListVo;
    }

    @Override
    public void addCompanyOpponent(CompanyOpponentAddVo companyopponentAddVo) {
        CompanyOpponentEntity companyopponentEntity = new CompanyOpponentEntity();
        companyopponentEntity.setId(UUIDUtils.getUUID());
        BeanUtils.copyProperties(companyopponentAddVo, companyopponentEntity);
        companyOpponentRepository.save(companyopponentEntity);

    }

    @Transactional
    @Override
    public void del(TradeDDelBody id) {
        companyOpponentRepository.deleteByTradeDIdAndCompanyId(id.getId(), id.getCompanyId());
    }

    @Override
    public void delList(DelListBody idList) {
        for(String id : idList.getList()) {
            companyOpponentRepository.delete(id);
        }
    }

    @Override
    public void modify(CompanyOpponentModVo companyopponentModVo){
        CompanyOpponentEntity companyopponentEntity = companyOpponentRepository.findOne(companyopponentModVo.getId());

        if (companyopponentEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_INCORRECT.getCode(), "没有对应信息");
        }
        TradeDEntity tradeDEntity = tradeDRepository.findOne(companyopponentModVo.getTradeDId());

        BeanUtils.copyProperties(companyopponentModVo, companyopponentEntity);
        companyopponentEntity.setTradeDName(tradeDEntity.getName());
        companyOpponentRepository.save(companyopponentEntity);
    }
}
